package com.smart.builder.xml;

import java.util.Date;
import java.util.List;

import com.smart.hotelsmart.InfoHotelResponseType;
import com.smart.hotelsmart.ListImageType;
import com.smart.hotelsmart.ListRoomType;
import com.smart.hotelsmart.RoomType;
import com.smart.persistence.entity.Hotel;
import com.smart.persistence.entity.Image;
import com.smart.persistence.entity.RoomModel;
import com.smart.persistence.service.SmartBasicCrudDAO;
import com.smart.service.util.Logging;
import com.smart.service.util.UtilDate;
import com.smart.service.util.UtilLink;

public class BuilderInfoHotelResponseType {
	
	/** A Constant LOGGER. */
	private static final Logging LOGGER = Logging
			.getLogger(BuilderInfoHotelResponseType.class.getName());
	
	/**
	 * generates the response from the data stored in the database note: it is
	 * calculated the number of rooms available for the period indicated
	 * 
	 * @param hotel
	 * @param arrivalDate
	 * @param departureDate
	 * @return
	 */
	public InfoHotelResponseType generateInfoHotelResponseFromEntity(
			Hotel hotel, Date arrivalDate, Date departureDate) {
		assert hotel != null;
		LOGGER.debug("generateInfoHotelResponseFromEntity " + 
				UtilDate.formatDate(arrivalDate != null ? arrivalDate : new Date(System.currentTimeMillis())) + 
				"  " + 
				UtilDate.formatDate(departureDate));
		InfoHotelResponseType response = new InfoHotelResponseType();
		
		response.setCep(hotel.getCep());
		response.setCidade(hotel.getCidade());
		response.setCodeHotel(hotel.getCodHotel());
		response.setEndereco(hotel.getEndereco());
		response.setLatitude(hotel.getLatitude());
		response.setLongitude(hotel.getLongitude());
		response.setQuality(hotel.getQuality());
		response.setNameHotel(hotel.getName());
		response.setLinkSelection(hotel.getLinkSelection());
		response.setLinkImage(hotel.getLinkImage());
		List<Image> list = hotel.getListImage();
		ListImageType listImageType = createListImage(list);
		response.setListImage(listImageType);
		response.setListRoom(generateListRoomAvailableForPeriodByRoomType(hotel,
			arrivalDate, departureDate));
		return response;

	}

	private ListImageType createListImage(List<Image> list) {
		ListImageType listImageType = new ListImageType();
		for (int i = 0; i < list.size(); i++) {
			listImageType.getImage()
					.add(UtilLink.getLinkImage(list.get(i).getLink()));
		}
		return listImageType;
	}
	
	private ListRoomType generateListRoomAvailableForPeriodByRoomType(Hotel hotel,
			Date arrivalDate, Date departureDate) {
		ListRoomType listRoom = new ListRoomType();
		// Get the entity RoomType and the amount available for the period
		// (arrivalDate<------->departureDate)
		List<Object[]> count = SmartBasicCrudDAO.getInstance()
				.consultaQueryList(SmartBasicCrudDAO.getInstance().getEntityManagerDAO(), Object[].class,
						RoomModel.findAvailableInPeriodByCodHotel,
						hotel.getCodHotel(), arrivalDate, departureDate);
		for (int i = 0; i < count.size(); i++) {
			RoomModel room = (RoomModel) count.get(i)[0];
			Integer amountAvailable = ((Long) count.get(i)[1]).intValue();
			listRoom.getRoom()
					.add(createRoomType(room, amountAvailable));
		}
		return listRoom;
	}
	
	
	
	
	private RoomType createRoomType(RoomModel room, Integer amountAvailable) {
		RoomType roomType = new RoomType();
		assert room.getId() != 0;
		roomType.setId(room.getId());
		roomType.setDescription(room.getDescription());
		roomType.setDetails(room.getDetails());
		roomType.setImage(UtilLink.getLinkImage(room.getImage()));
		roomType.setInfo(room.getInfo());
		roomType.setName(room.getName());
		roomType.setPrice(room.getPrice());
		roomType.setCodeRoom(room.getCodRoom());
		roomType.setAmountAvailable(amountAvailable);
		return roomType;
	}


}
